<?php

App::uses('AppController', 'Controller');

class ProductsController extends AppController {

	var $name = 'Products';
	var $uses = array('Product', 'Image');



	/**
	 * Danh Sach san pham
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function lists($category_id) {

		App::uses('Category', 'Model');
		$Category = new Category();

		$view = new View( $this );
        $Text = $view->loadHelper( 'Text' );

		$products = $this->Product->getAll( (int)$category_id);
		if(!empty($products))
		foreach($products as $k => $v) {
			$v['Product']['image'] = $this->Image->getOneNameImage('Product-'.$v['Product']['id']);
			$v['Product']['desc'] = $Text->truncate($v['Product']['desc'], 40, array('ellipsis' => '...', 'exact' => false, 'html' => true));
			$products[$k] = $v;
		}

		$category = $Category->read(array('id', 'name', 'desc', 'parent_id'), (int)$category_id);

		$this->set('images', $this->Image->getAll('Category-'.$category_id));
		$this->set('products', $products);
		$this->set('items', $Category->getAll( $category['Category']['parent_id']));
		$this->set('cate_id', $category['Category']['parent_id']); // phai truyen qua de kiem tra menu
		$this->set('category', $category);
		$this->set('als', 'Product');
	}

	/**
	 * Xem chi tiet
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function detail($id)
	{
		App::uses('Category', 'Model');
		$Category = new Category();
		$info = $this->Product->read(array(), (int)$id);
		$category = $Category->read(array(), $info['Product']['category_id']);

		$this->set('product', $info);
		$this->set('images', $this->Image->getAll($id));
		$this->set('cate_id', $category['Category']['parent_id']);
		$this->set('category', $category);
		$this->set('items', $Category->getAll( $category['Category']['parent_id']));
	}



	/**
	 * Danh sach san pham
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function admin_index( $parent_id = 0, $category_id = null) {
		App::uses('Category', 'Model');
		$Category = new Category();

		// Lấy danh mục cha
		$parentcates = $Category->getList();
		if($parent_id === 0) {
			$parent_id  = array_keys($parentcates);
			$parent_id = $parent_id[0];
		}

		// Lay danh muc con
		$categories = $Category->getList($parent_id);
		if(empty($category_id) && !empty($categories)){
			$category_id = array_keys($categories);
			$category_id = $category_id[0];
		}

		$this->paginate = array( 'order'=> array('sort' => 'asc'));

        $items = $this->paginate(array('category_id' => $category_id, 'status' => 4));

		$this->set('cate_id', $parent_id);
		$this->set('category_id', $category_id);
		$this->set('categories', $categories);
		$this->set('parent_categories', $parentcates);
		$this->set('items', $items);
	}

	/**
	 * Add
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function admin_add( $parent_id, $category_id, $id = null)
	{
		if(!empty($this->request->data))
		{
			$data = &$this->request->data[$this->als];

			$this->Product->set($data);
			$errors = $this->Product->invalidFields();

			//Save
			if(empty($errors)) {

				// kiem tra edit
				if( empty($data['id']) ) {
					unset($data['id']);
				} else {
					$data['id'] = (int)$data['id'];
				}

				$data['status'] = 4;

				if($this->Product->save($data, false)) {

					// Xoa hinh anh cu
					if(!empty($data['image_del'])) {
						$this->Common->_delImageByListId($data['image_del'], $this->Image);
					}

					// Luu hinh anh
					if(!empty($data['images'])) {
						$image = array( 'target_id' => 'Product-'.$this->Product->id );
						foreach($data['images'] as $img) {
							if(!empty($img['name'])) {
								$image['image'] = $this->_upload($img);

								if(!empty($image['image'])) {
									$this->Image->create();
									$this->Image->save($image);
								}
							}
						}
					}

					$this->Session->setFlash('Lưu sản phẩm thành công.', 'success');
					$this->redirect('/admin/products/index/'.$this->request->data['cate_id'].'/'.$data['category_id']);
				} else {
					$this->Session->setFlash('Lưu sản phẩm thất bại. Vui lòng xem lại thông tin bên dưới', 'error');
				}
			} else {
				$this->Session->setFlash('Lưu sản phẩm thất bại.', 'error');
			}
		}

		// Truong hop show edit
		if( !empty( $id )) {
			$info = $this->Product->getInfoById($id);
			if(!empty($info)) {
				$this->request->data = $info;
				$this->set('images', $this->Image->getAll('Product-'.$id));
			}
		}
		App::uses('Category', 'Model');
		$Category = new Category();

		$this->set('categories', $Category->getList($parent_id));
		$this->set('cate_id', $parent_id);
		$this->set('category_id', $category_id);
	}

	/**
	 * View Image
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function admin_images($parent_id, $category_id, $id) {
		$info = $this->Product->getInfoById($id, array('id', 'name'));
		$this->set('images', $this->Image->getAll('Product-'.$id));
		$this->set('product', $info['Product']);
		$this->set('category_id', $category_id);
		$this->set('cate_id', $parent_id);
	}

	/**
	 * Remove img
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function admin_deleteimg() {

		if(!empty($this->request->data['id'])) {
			$this->Common->_delImageByListId(array((int)$this->request->data['id']));
			$this->_responseJson(array('code' => 'success', 'msg' => 'Xóa hình ảnh thành công.'));
		}
		$this->_responseJson(array('code' => 'danger', 'msg' => 'Xóa hình ảnh thất bại.'));
	}

	/**
	 * Change Sort
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function admin_changeSort() {
		if(!empty($this->request->data['id']) && !empty($this->request->data['sort'])) {
			$this->Product->id = (int)$this->request->data['id'];

			if($this->Product->saveField('sort', (int)$this->request->data['sort']))
				$this->_responseJson(array('code' => 'success'));
			else
				$this->_responseJson(array('code' => 'danger'));
		}
		$this->_responseJson(array('code' => 'danger'));
	}

	/**
	 * delete san pham
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function admin_delete($parent_id, $category_id, $id = null){
		if(!empty($id)) {
			$id = (int)$id;
			$info = $this->Product->getInfoById($id, array('category_id'));
			if($this->Product->delete($id)) {
				// Xoa images
				$this->Common->_delImageByTargetId($id, $this->Image);

				$this->Session->setFlash('Xóa sản phẩm thành công.', 'success');
			} else {
				$this->Session->setFlash('Xóa sản phẩm thất bại. Vui lòng thử lại sao', 'error');
			}
		} else {
			$this->Session->setFlash('Không tìm thấy sản phẩm. Vui lòng thử lại sao', 'error');
		}
		$this->redirect('/admin/products/index/'.$parent_id.'/'.$category_id.'/'.$info['Product']['category_id']);
	}
}
